package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void addCart(ShoppingCartDTO dto) {
        // 1.构造购物车对象数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);

        // 2.添加商品到表中之前，先判断该商品是否已经存在了，调用mapper的查询方法（根据用户id+商品id）
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> carts = shoppingCartMapper.listBy(shoppingCart);

        // 3 如果同样的商品已存在，那么只需要更新商品的数量（数量+1），调用mapper的update方法
        if (carts != null && !carts.isEmpty()) {
            ShoppingCart cart = carts.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }else {
            // 4 否则，调用mapper的新增方法，插入购物车数据到shopping_cart表中
            // 需要补充字段值
            // 判断添加是菜品还是套餐
            if (dto.getDishId() != null) {//代表添加菜品
                //4.1根据菜品id查询菜品数据
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else { //代表添加套餐
                //4.2根据套餐id查询套餐数据
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //注意：只能查询当前用户的购物车，所以需要获取当前用户id
        return shoppingCartMapper.getByUserId(BaseContext.getCurrentId());
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //注意：只能清空自己购物车里面的数据
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> cartDBList = shoppingCartMapper.listBy(shoppingCart);

        if(cartDBList != null && !cartDBList.isEmpty()){
            ShoppingCart cartDB = cartDBList.get(0);
            Integer number = cartDB.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(cartDB.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                cartDB.setNumber(cartDB.getNumber() - 1);
                shoppingCartMapper.update(cartDB);
            }
        }
    }
}
